<?php
	defined( "AZ_EXE_" ) or die ( "CORE VARIABLE IS NOT DEFINED." );
	function substr_unicode($str, $s, $l = null) {
		return join("", array_slice(
				preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY), $s, $l));
	}
	function spiteTitleWord($string = "",$num_word = 5){
		$words = str_word_count($string,1);
		if(count($words) <= $num_word) 
			return $string;
		$sentence = "";
		for($i = 0;$i < $num_word;$i++){
			$sentence .= $words[$i] . " ";
		}
		$sentence = rtrim($sentence," ") . "...";
		return $sentence;
	}
	function spiteTitleLetter($string = "",$num_letter = 50){
		if(strlen($string) <= $num_letter)
			return $string;
		else return substr($string,0,$num_letter) . "...";
	}
	function getDuration($created = ""){		
		date_default_timezone_set("Asia/Bangkok");
		$create = abs(strtotime(date("Y-m-d H:i:s")) - strtotime($created));
		$years = floor($create / (365*60*60*24));
		$months = floor(($create - ($years * 365*60*60*24)) / (30*60*60*24));
		$days = floor(($create - ($years * 365*60*60*24) - ($months*30*60*60*24) )/ (60*60*24));
		$hour = floor(($create - ($years * 365*60*60*24) - ($months*30*60*60*24) - ($days*60*60*24) )/ (60*60));
		$min = floor(($create - ($years * 365*60*60*24) - ($months*30*60*60*24) - ($days*60*60*24) - ($hour*60*60) )/ (60));
		
		
		$created = new DateTime($created);
		$now = new DateTime(date( 'Y-m-d H:i:s'));
		$difference = $created->diff($now);
		$year = $difference->y;
		$month = $difference->m;
		$day = $difference->d;
		$hour = $difference->h;
		$minute = $difference->i;
		$second = $difference->s;
		$days = $difference->days;
		
		if($days <= 0){
			if($hour <= 0){
				$duration = $min . ' min ago';
			}elseif($hour == 1){
				$duration = $hour . ' hour ' . $min . ' min ago';
			}elseif($hour > 1){
				$duration = $hour . ' hours ' . $min . ' min ago';
			}
		}else{
			if($days == 1){
				if($hour <= 1){
					$duration = $days . ' day ' . $hour . ' hour ' . $min . ' min ago';
				}else{
					$duration = $days . ' day ' . $hour . ' hours ' . $min . ' min ago';
				}
			}else{
				if($hour <= 1){
					$duration = $days . ' days ' . $hour . ' hour ' . $min . ' min ago';
				}else{
					$duration = $days . ' days ' . $hour . ' hours ' . $min . ' min ago';
				}
			}
		}
		return $duration;
	}	
	function convert2kh($str = ""){
		if($str == "") return 0;
		else{
			if($_SESSION['lang'] == "kh"){
				$numbers = array(0 => "០",1 => "១", 2 => "២", 3 => "៣", 4 => "៤", 5 => "៥", 6 => "៦", 7 => "៧", 8 => "៨", 9 => "៩");
				$tmp = "";
				$cur = "";
				for($i = 0; $i < strlen($str); $i++){
					$cur = substr($str,$i,1);
					$tmp .= $numbers[$cur];
				}
				return $tmp;
			}else return $str;
		}
		
	}
	function isEmail($str = ""){
		if($str != ""){
			$pattern = "/^([a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4})?$/";
			if(preg_match($pattern,$str)) return 1;
			else return 0;
		}else return 0;
	}